using System;
using System.Collections.Generic;
using Brouteforce.Screens.CreateNewCharacterScreen;
using Brouteforce.Screens.MenuScreen;
using Brouteforce.Screens.PlayingScreen;
using Brouteforce.UserInterface.Abstracts;
using Brouteforce.UserInterface.Interfaces;
using Microsoft.Xna.Framework;
using Brouteforce.Utilities;
using Microsoft.Xna.Framework.Content;
using Brouteforce.Screens.PlayingScreen.Panels;

namespace Brouteforce.NewManagers {
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class ScreenManager : Manager {
        public static ScreenManager Instance = new ScreenManager();

        public Stack<IScreen> Screens = new Stack<IScreen>();
        public MonitoredProperty<IScreen> CurrentScreen = new MonitoredProperty<IScreen>(null);
        private bool _initialized = false;


        public override void Draw(GameTime gameTime, BFGame game) {
            if (Screens.Count > 0) {
                Screens.Peek().Draw(gameTime);
            }
        }

        protected override void _Update(GameTime gameTime, BFGame game) {
            if (Screens.Count > 0) {
                Screens.Peek().Update(gameTime);
            }
        }

        public override void LoadContent(ContentManager content) {
            GamePanel.LoadContent(content);
        }

        public override void Initialize() {
            _initialized = true;
            var firstScreen = new PlayingScreen();
            firstScreen.AfterLayout();
            PushScreen(firstScreen);

            
        }


        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public void Update(GameTime gameTime) {
            //todo check _lastKnowedLoopIndex
            Peek().Update(gameTime);
        }


        public void Draw(GameTime gameTime) {
            Peek().Draw(gameTime);
        }


        public void PushScreen(AbstractScreen newScreen) {
            if (!_initialized) throw new Exception("not initialized yet");
            Screens.Push(newScreen);
            CurrentScreen.Property = newScreen;
        }

        public void OpenNewCharacterScreen() {
            AbstractScreen createNewCharacterScreen = new CreateNewCharacterScreen();
            PushScreen(createNewCharacterScreen);
            createNewCharacterScreen.AfterLayout();
        }

        public void CloseNewCharacterScreen(CreateNewCharacterPanel createNewCharacterPanel) {
            //Todo add the logic of adding a new character
            //like this createNewCharacterPanel.LifeSlider.GetSliderValue;
            
            if (Peek() is CreateNewCharacterScreen) {
                PopScreen();
            }
        }

        public IScreen PopScreen() {
            var screen = Screens.Pop();
            CurrentScreen.Property = Peek();

            return screen;
        }

        public IScreen Peek() {
            return Screens.Peek();
        }

    }
}
